﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Estimate
{
    public partial class formBudgetObject : FormMDIChildTemplate
    {
        public formBudgetObject()
        {
            InitializeComponent();
        }

        private void formBudgetObject_Load(object sender, EventArgs e)
        {
            firmBindingSource.DataSource = context.Firms.Where(x => x.FirmTypeID == 3);
            rateBindingSource.DataSource = context.Rates.Where(x => x.RateID >= 9 && x.RateID <= 11);
            LoadObjects();
            root.TranslateGrid(grid);
            root.TranslateGrid(gridWorkCategory);
        }

        private void LoadObjects()
        {
            objectBindingSource.DataSource =
                context.Objects.OrderByDescending(x => x.ContractDate).Where(x => x.IsReal == true);
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            Object obj = (Object)objectBindingSource.AddNew();
            obj.IsReal = true;
            obj.IsTaxSingle = true;
            obj.TaxVat = context.Rates.Where(x => x.RateID == 1).Select(x => x.RateValue).First();
            obj.TaxCurrent  = context.Rates.Where(x => x.RateID == 2).Select(x => x.RateValue).First();
            obj.TaxStorehouse  = context.Rates.Where(x => x.RateID == 3).Select(x => x.RateValue).First();
            obj.TaxTransport = context.Rates.Where(x => x.RateID == 4).Select(x => x.RateValue).First();
            obj.TaxAdmin = context.Rates.Where(x => x.RateID == 5).Select(x => x.RateValue).First();
            obj.TaxProfit = context.Rates.Where(x => x.RateID == 6).Select(x => x.RateValue).First();
            //obj.TaxSingle  = context.Rates.Where(x => x.RateID == 7).Select(x => x.RateValue).First();
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            this.ActiveControl = buttonPrint;
            root.Submit(); 
            LoadObjects();
        }

        private void grid_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
        {
            string value = e.Value as string;
            string columnName = grid.Columns[e.ColumnIndex].Name.ToUpper();
            if (columnName.Contains("FIRM"))
            {
                Firm firm = (firmBindingSource.OfType<Firm>().Where(x => x.ToString() == value)).FirstOrDefault();
                e.Value = firm;
            }
            else if (columnName.Contains("RATE"))
            {
                Rate rate =
                    (rateBindingSource.OfType<Rate>().Where(x => x.ToString() == value)).FirstOrDefault();
                e.Value = rate;
            }

            e.ParsingApplied = true;
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            Object obj = (Object)objectBindingSource.Current;
            if (obj != null || obj.BudgetProducts.Count > 0 || obj.ObjectWorkCategories.Count > 0 ||
                obj.InvoiceOutcomes.Count > 0 || obj.ObjectWorkCategories.Count > 0)
            { formMain.ShowError("Неможливо видалити обєкт"); }
            else
            {
                objectBindingSource.RemoveCurrent();
                root.Submit(false);
                LoadObjects();
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            reportAgreement report = new reportAgreement();
            Object objectAgreemant = (Object)objectBindingSource.Current;
          
            report.xrLabelNumber.Text = objectAgreemant.ContractNumber;
            report.xrLabelDate.Text = objectAgreemant.ContractDate.Value.ToLongDateString();
            report.xrLabelZamovnyk.Text = objectAgreemant.Firm.FirmName;
            report.xrLabelPostachalnyk.Text = db.GetFirmOwn().FirmName;
            report.xrLabelDyrektor.Text = db.GetFirmOwn().Director;
            report.xrLabelWorkName.Text = objectAgreemant.ObjectName;
            report.xrLabelZamovnykALL.Lines = objectAgreemant.Firm.GetFullDescription();
            report.xrLabelPostachalnykAll.Lines = db.GetFirmOwn().GetFullDescription();
            report.ShowPreview();
        }

        private Object currentObject;
        private void objectBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            currentObject = (Object) objectBindingSource.Current;
            if (currentObject != null)
            {
                objectWorkCategoryBindingSource.DataSource =
                    context.ObjectWorkCategories.Where(x => x.Object == currentObject);
                buttonBudget.Enabled = true; buttonAVR .Enabled = true; buttonPrint .Enabled = true;
            }
            else { buttonBudget.Enabled = false ; buttonAVR.Enabled = false ; buttonPrint.Enabled = false ; }
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            if (gridWorkCategory.Rows.Count == 3) return;
            ObjectWorkCategory workCategory = (ObjectWorkCategory) objectWorkCategoryBindingSource.AddNew();
            if (currentObject != null) workCategory.Object = currentObject;
            workCategory.ObjectWorkCategoryName = "Нова Категорія";
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            root.Submit();
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            ObjectWorkCategory workCategory = (ObjectWorkCategory) objectWorkCategoryBindingSource.Current;
            if (workCategory != null)
            {
                if (workCategory.ObjectWorks.Count > 0) return;
                objectWorkCategoryBindingSource.RemoveCurrent();
            }
        }

        private void buttonAVR_Click(object sender, EventArgs e)
        {
            formBudgetWorkCompete formBudgetWorkCompete = new formBudgetWorkCompete(currentObject);
            root.ShowForm(formBudgetWorkCompete);
        }

        private void buttonBudget_Click(object sender, EventArgs e)
        {
            formBudgetCreate formBudgetCreate = new formBudgetCreate(currentObject);
            root.ShowForm(formBudgetCreate);
        }
    }
}
